﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel.Configuration;
using System.ServiceModel;
using System.ServiceModel.Routing;

namespace Cherry.Agile.ServiceModel.Routing {

    public sealed class ServiceRoutingBehavior : BehaviorExtensionElement, IServiceBehavior {
        public override Type BehaviorType {
            get { throw new NotImplementedException(); }
        }

        protected override object CreateBehavior() {
            throw new NotImplementedException();
        }

        public void AddBindingParameters(ServiceDescription serviceDescription,
            System.ServiceModel.ServiceHostBase serviceHostBase, 
            System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, 
            System.ServiceModel.Channels.BindingParameterCollection bindingParameters) {
            
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, 
            System.ServiceModel.ServiceHostBase serviceHostBase) {

            RulesUpdateExtension rulesUpdateExtension = new RulesUpdateExtension();
            serviceHostBase.Extensions.Add(rulesUpdateExtension);
        }

        public void Validate(ServiceDescription serviceDescription,
            System.ServiceModel.ServiceHostBase serviceHostBase) {
             
        }
    }

    class RulesUpdateExtension : IExtension<ServiceHostBase>{

        public void Attach(ServiceHostBase owner) {
            RoutingConfiguration rc = new RoutingConfiguration();
            rc.FilterTable.Add(new ServiceMessageFilter(), new List<ServiceEndpoint>());
            owner.Extensions.Find<RoutingExtension>().ApplyConfiguration(rc);
        }

        public void Detach(ServiceHostBase owner) {
            
        }
    }
}
